package services;

import java.util.List;

import javax.inject.Inject;

import models.RouteRequest;
import resulttypes.RouteRequestVO;
import siena.PersistenceManager;
import siena.PersistenceManagerFactory;

import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.GeocellQuery;
import com.google.inject.AbstractModule;

import extensions.com.beoui.geocell.SienaGeocellQueryEngine;

public class SharedRouteServiceImpl extends AbstractModule implements
		SharedRouteService {
	private PersistenceManager pm;
	private GeocellManager gm;

	public RouteRequest saveRouteRequest(RouteRequestVO vo) {
		RouteRequest dbObj = RouteRequest.fromVO(vo);
		dbObj.insert();
		return dbObj;
	}

	@Override
	public List<RouteRequest> findCandidates(RouteRequest req) {
		GeocellQuery q = new GeocellQuery();
		SienaGeocellQueryEngine qe = new SienaGeocellQueryEngine(pm);
		// TODO => 1000m as configurable parameter
		List<RouteRequest> result = gm.proximitySearch(req.getLocation(), 10,
				1000d, RouteRequest.class, q, qe,
				GeocellManager.MAX_GEOCELL_RESOLUTION - 1);

		return result;
	}

	public SharedRouteServiceImpl() {
		pm = PersistenceManagerFactory
				.getPersistenceManager(RouteRequest.class);
		gm = new GeocellManager();
	}

	@Override
	protected void configure() {
		bind(SharedRouteService.class).to(SharedRouteServiceImpl.class);
	}
}
